/*
 * @Descripttion: 
 * @Author: MajorMayer
 * @Date: 2021-03-17 10:22:42
 * @LastEditors: MajorMayer
 * @LastEditTime: 2021-07-27 14:25:24
 */
import { forOwn } from 'lodash'
import to from 'await-to-js'

import MoreArgsInject from './moreArgsInject'
import { get, post, postByQs, postByJson,postByFormData } from "./http"
import { http } from "./httpAny"

import otherUrl from "./apiUrl"
import apiUrl from "./apiUrl/api"

export { checkResponse } from './vertify'

function apiUrlFactory() {
    let apiUrlObject = {}
    forOwn({ ...apiUrl, ...otherUrl }, function ([uri, method, moreArgs], key) {
        apiUrlObject[key] = method == 'HTTPANY'
            ? configObj => http(uri, configObj)
            : async (params, token) => {
                var httpFn = [get, post, postByQs, postByJson,postByFormData][['GET', 'POST', 'POSTQS', 'POSTJSON','POSTFORMDATA'].indexOf(method)]
                var [err, params] = await to(MoreArgsInject(params, moreArgs))
                if (err) return Promise.reject(err || Error('程序参数处理异常,模块:MoreArgsInject'));
                return httpFn(uri, params, token)
            }
    })
    return apiUrlObject
}

export default apiUrlFactory()